package web

import (
	"github.com/gookit/validate"
	"ksd-social-api/commons/base/controller"
	"ksd-social-api/modules/special/model/context"
)

type SpecialCommentReplyController struct {
	controller.BaseController
}

/**
 * 保存专题评论
 * @author feige
 * @date 2023-11-20
 * @version 1.0
 * @desc
 */
func (web *SpecialCommentReplyController) SaveComment() {
	// 1: 绑定传递过来的参数
	var specialCommentContext context.SpecialCommentContext
	web.BindJSON(&specialCommentContext)
	// 2: 开始验证参数
	validation := validate.Struct(specialCommentContext)
	if !validation.Validate() {
		web.FailWithValidatorData(validation)
		return
	}

	// 3：查询专题的基础信息
	detail, err := specialService.GetSpecialByID(specialCommentContext.Opid.Uint64(), web.GetUserId(), web.GetSystemId())
	if nil != err {
		web.FailCodeMsg(601, "评论下载不存在！")
		return
	}

	// 4: 绑定用户参数，不建议从页面端传递获取，直接在服务端获取更加安全
	specialCommentContext.Title = detail.Title
	specialCommentContext.UserId = web.GetUserId()
	specialCommentContext.Username = web.GetUsername()
	specialCommentContext.Nickname = web.GetUserNickname()
	specialCommentContext.Uuid = web.GetUuid()
	specialCommentContext.VipType = web.GetUserVipType()
	specialCommentContext.Avatar = web.GetUserAvatar()
	specialCommentContext.SystemId = web.GetSystemId()
	specialCommentContext.SystemName = web.GetSystemName()
	// 5: 做频率限制
	commentLimit := specialCommentReplyService.CheckLimitComment(web.GetUserId(), web.GetSystemId())
	if commentLimit != nil {
		web.FailCodeMsg(601, commentLimit.(string))
		return
	}

	// 6: 执行保存评论
	specialCommentContext.Id = web.GetSnowWorkerId(5)
	flag := specialCommentReplyService.SaveCommentReply(specialCommentContext)

	if !flag {
		web.FailCodeMsg(601, "保存评论失败!!")
		return
	}
	// 把评论的上下文返回即可
	web.Ok(specialCommentContext)
}

/**
 * 根据parentId和opid查询对应的子回复
 * @author feige
 * @date 2023-11-21
 * @version 1.0
 * @desc
 */
func (web *SpecialCommentReplyController) FindCommentList() {
	// 1: 绑定传递过来的参数
	var commentReplyPageContext context.CommentReplyPageContext
	web.BindJSON(&commentReplyPageContext)
	// 2: 开始验证参数
	validation := validate.Struct(commentReplyPageContext)
	if !validation.Validate() {
		web.FailWithValidatorData(validation)
		return
	}
	commentReplyPageContext.SystemId = web.GetSystemId()
	// 查询返回
	page := specialCommentReplyService.FindCommentAndPage(commentReplyPageContext)
	web.Ok(page)
}

/**
 * 根据用户id, 专题id，和评论id删除对应用户的评论
 * @author feige
 * @date 2023-11-21
 * @version 1.0
 * @desc
 */
func (web *SpecialCommentReplyController) DelComment() {
	// 1: 绑定传递过来的参数
	var commentDelContext context.CommentDelContext
	web.BindJSON(&commentDelContext)
	// 2: 开始验证参数
	validation := validate.Struct(commentDelContext)
	if !validation.Validate() {
		web.FailWithValidatorData(validation)
		return
	}

	commentDelContext.UserId = web.GetUserId()
	commentDelContext.SystemId = web.GetSystemId()
	// 查询返回
	bool := specialCommentReplyService.DelCommentByIdAndUserId(commentDelContext)
	web.Ok(bool)
}
